package net.samsb.tool;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;

import net.samsb.BaseApplication;
import net.samsb.R;
import net.samsb.activity.Constants;
import net.samsb.model.DoorEntity;
import net.samsb.view.WheelView;

import java.util.ArrayList;
import java.util.List;

public class MyDialog {

    public static void showDialog(final Context context, final String domain, final String port,
                                  final List<DoorEntity> doorEntities,
                                  final ProgressDialog waitingDialog, AlertDialog builder) {

        View myView = LayoutInflater.from(context).inflate(R.layout.activity_dialog, null);
        final LoopView mLoopView = (LoopView) myView.findViewById(R.id.wheelView);

        builder.setIcon(R.drawable.icon_setting_2);
        builder.setTitle("请选择检测大门");
        builder.setView(myView);

        final List<String> lists = new ArrayList<>();

        for (DoorEntity doorEntity : doorEntities) {
            lists.add(doorEntity.getCrkzwmc());
        }

        mLoopView.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                Log.e("wheelView", "current index :" + index + " ,result=" + lists.get(index));

                String cookies = doorEntities.get(index).getCrkbh() + "-" + BaseApplication.BaseCookiesTimeS;

                //更换ip、端口
                if (BaseApplication.BaseWebSocket != null) {
                    BaseApplication.BaseWebSocket.close(4444, null);
                }

                waitingDialog.show();
                BaseApplication
                        .BaseRequestInstance
                        .getWebSocketRequest()
                        .getWebSocketMessage(domain, port, cookies, waitingDialog);

                Constants.URL_COOKIES = cookies;
                BaseApplication.isFirstOpen = true;
                BaseApplication.mEditor.putString("cookies", cookies).commit();
            }
        });

        mLoopView.setItems(lists);
        mLoopView.setInitPosition(0);

        builder.show();
    }


    public static void showDialog(Context context, String message) {

        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        builder.setTitle("提示");
        builder.setMessage(message);

        builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {

            }
        });

        builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {

            }
        });
//                .setNeutralButton("button3", new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int id) {
//
//                    }
//                 })

        builder.create();
        builder.show();
    }

    public static void dismissDialog(AlertDialog builder) {
        builder.dismiss();
    }
}
